package hlhj.fhp.supreme.utils

import android.content.Context
import hlhj.fhp.supreme.javabean.UserInfoBean
import android.R.id.edit
import android.util.Base64
import java.io.*


/**
 * Created by Administrator on 2018\4\3 0003.
 */
object SPHelper {
    private val TOKEN="token"

    private val LoginStatus="loginStatus"

    private val LOGININFO="loginInfo"
    private val UserInfo="userinfo"
    private val ISFIRST="isfirst"
    /*是否第一次登录*/
    fun saveIsFirst(context: Context,b:Boolean){
        val sharedPreferences = context.getSharedPreferences(ISFIRST, Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putBoolean("isFirst",b)
        editor.apply()
    }
    fun getIsFirst(context: Context): Boolean {
        val sharedPreferences = context.getSharedPreferences(ISFIRST, Context.MODE_PRIVATE)
        return sharedPreferences.getBoolean("isFirst",true)
    }
    /*TOken*/
    fun saveToken(context: Context,token:String){
        val sharedPreferences = context.getSharedPreferences(TOKEN, Context.MODE_PRIVATE)

        val editor = sharedPreferences.edit()
        editor.putString("token", token)
        editor.apply()

    }

    fun getToken(context: Context) :String{
        val sp = context.getSharedPreferences(TOKEN, Context.MODE_PRIVATE)
        return sp.getString(TOKEN,"")
    }

    /*登录状态*/

    fun saveLoginStatus(context: Context,boolean: Boolean){
        val sp = context.getSharedPreferences(LoginStatus, Context.MODE_PRIVATE)

        val edit = sp.edit()
        edit.putBoolean(LoginStatus,boolean)
        edit.apply()
    }

    fun getLoginStatus(context: Context): Boolean {
        val preferences = context.getSharedPreferences(LoginStatus, Context.MODE_PRIVATE)
        return preferences.getBoolean(LoginStatus,false)
    }

    /*用户名、密码*/
    fun saveUserInfo(context: Context,userName:String,userPwd:String){
        val preferences = context.getSharedPreferences(LOGININFO, Context.MODE_PRIVATE)
        val edit = preferences.edit()
        edit.putString("username",userName)
        edit.putString("userpwd",userPwd)
        edit.apply()
    }
    fun getUserName(context: Context): String? {
        val preferences = context.getSharedPreferences(LOGININFO, Context.MODE_PRIVATE)
        return preferences.getString("username",null)
    }
    fun getPwd(context: Context): String? {
        val preferences = context.getSharedPreferences(LOGININFO, Context.MODE_PRIVATE)
        return preferences.getString("userpwd",null)
    }
    /*用户信息*/
    fun saveUser(context: Context,userInfoBean: Any){
        val sp = context.getSharedPreferences(UserInfo, Context.MODE_PRIVATE)
        val edit = sp.edit()

        if (userInfoBean is Serializable){
            val baos = ByteArrayOutputStream()
            val oos = ObjectOutputStream(baos)
            oos.writeObject(userInfoBean)
            val string64 = String(Base64.encode(baos.toByteArray(),
                    0))
            edit.putString(UserInfo, string64).apply()
        }
    }

    fun getUser(context: Context): Any? {
        var obj:Any
        val base64 = context.getSharedPreferences(UserInfo,Context.MODE_PRIVATE).getString(UserInfo, "")
        if (base64 == "") {
            return null
        }
        val base64Bytes = Base64.decode(base64.toByteArray(), 1)
        val bais = ByteArrayInputStream(base64Bytes)
        val ois = ObjectInputStream(bais)
        obj = ois.readObject()
        return obj
    }

    /*七牛TOken*/

    fun saveQNtoken(context: Context,token: String){
        val sp = context.getSharedPreferences("qiniu", Context.MODE_PRIVATE)
        val edit1 = sp.edit()
        edit1.putString("token",token)
        edit1.apply()
    }

    fun readQntoken(context: Context): String? {
        val sp = context.getSharedPreferences("qiniu", Context.MODE_PRIVATE)
        return sp.getString("token",null)
    }
}